# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
from itemadapter import ItemAdapter
import json


class BookSpiderPipeline:
    def open_spider(self, spider):
        """当爬虫启动时调用"""
        self.file = open('books.json', 'w', encoding='utf-8')
        self.file.write('[\n')
        self.first_item = True

    def close_spider(self, spider):
        """当爬虫关闭时调用"""
        self.file.write('\n]')
        self.file.close()

    def process_item(self, item, spider):
        """处理每个item"""
        # 将item转换为字典
        adapter = ItemAdapter(item)
        item_dict = adapter.asdict()
        
        # 写入JSON文件
        if not self.first_item:
            self.file.write(',\n')
        else:
            self.first_item = False
            
        line = json.dumps(item_dict, ensure_ascii=False, indent=2)
        self.file.write(line)
        
        return item